﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ECatalog.Models;
using ECatalog.Models.UserModel;

namespace ECatalog.Controllers
{
    [Authorize]
    public class ServerConfigController : Controller
    {
        private readonly IUserRepository userRepository;
        private DbContainer db = new DbContainer();

        public ServerConfigController(IUserRepository userRepository)
        {
            this.userRepository = userRepository;
        }

        [Authorize]
        public ActionResult Index()
        {
            @ViewBag.IsAdmin = userRepository.IsAdmin(User.Identity.Name);
            var serverparams = db.ServerParams.Include(s => s.Organization).Include(s => s.Shop);
            return View(serverparams.ToList());
        }

        [Authorize]
        public ActionResult Create()
        {
            @ViewBag.IsAdmin = userRepository.IsAdmin(User.Identity.Name);
            ViewBag.OrganizationId = new SelectList(db.Organizations, "OrganizationId", "Name");
            ViewBag.ShopId = new SelectList(db.Shops, "ShopId", "Name");
            return View();
        }

        [Authorize]
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(ServerParam serverparam)
        {
            @ViewBag.IsAdmin = userRepository.IsAdmin(User.Identity.Name);
            if (ModelState.IsValid)
            {
                db.ServerParams.Add(serverparam);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.OrganizationId = new SelectList(db.Organizations, "OrganizationId", "Name", serverparam.OrganizationId);
            ViewBag.ShopId = new SelectList(db.Shops, "ShopId", "Name", serverparam.ShopId);
            return View(serverparam);
        }

        [Authorize]
        public ActionResult Edit(int id = 0)
        {
            @ViewBag.IsAdmin = userRepository.IsAdmin(User.Identity.Name);
            ServerParam serverparam = db.ServerParams.Find(id);
            if (serverparam == null)
            {
                return HttpNotFound();
            }
            ViewBag.OrganizationId = new SelectList(db.Organizations, "OrganizationId", "Name", serverparam.OrganizationId);
            ViewBag.ShopId = new SelectList(db.Shops, "ShopId", "Name", serverparam.ShopId);
            return View(serverparam);
        }

        [Authorize]
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(ServerParam serverparam)
        {
            @ViewBag.IsAdmin = userRepository.IsAdmin(User.Identity.Name);
            if (ModelState.IsValid)
            {
                db.Entry(serverparam).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.OrganizationId = new SelectList(db.Organizations, "OrganizationId", "Name", serverparam.OrganizationId);
            ViewBag.ShopId = new SelectList(db.Shops, "ShopId", "Name", serverparam.ShopId);
            return View(serverparam);
        }

        [Authorize]
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Delete(int id)
        {
            @ViewBag.IsAdmin = userRepository.IsAdmin(User.Identity.Name);
            ServerParam serverparam = db.ServerParams.Find(id);
            db.ServerParams.Remove(serverparam);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}